﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Seminarska
{
    class NumPad
    {
        private enum Operation
        {
            None,
            Subtract,
            OneX

        }
        private Operation LastOper;
        private string _display;
        private bool _erasediplay;
        private string _last_val;

        private string LastValue
        {
            get
            {
                if (_last_val == string.Empty)
                    return "0";
                return _last_val;

            }
            set
            {
                _last_val = value;
            }
        }

        private bool EraseDisplay
        {
            get
            {
                return _erasediplay;

            }
            set
            {
                _erasediplay = value;
            }
        }

        private string Display
        {
            get
            {
                return _display;
            }
            set
            {
                _display = value;
            }
        }

        private void ProcessKey(char c)
        {
            if (EraseDisplay)
            {
                Display = string.Empty;
                EraseDisplay = false;
            }
            AddToDisplay(c);
        }

        private void UpdateDisplay(string text)
        {
            if (Display == String.Empty)
                text = " ";
            else
                text = Display;
        }

        private void AddToDisplay(char c)
        {
            if (c == '.')
            {
                if (Display.IndexOf('.', 0) >= 0)  //already exists
                    return;
                Display = Display + c;
            }
            else
            {
                if (c >= '0' && c <= '9')
                {
                    Display = Display + c;
                }
                else
                    if (c == '\b')  //backspace ?
                    {
                        if (Display.Length <= 1)
                            Display = String.Empty;
                        else
                        {
                            int i = Display.Length;
                            Display = Display.Remove(i - 1, 1);  //remove last char 
                        }
                    }

            }

            UpdateDisplay(Display);
        }

        private void ProcessOperation(string s)
        {
            LastOper = Operation.None;
            Display = LastValue;
            UpdateDisplay(Display);

        }
    }
}
